﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using BizUserDALEntities;

namespace BizUserDAL
{
    public class BizDAL : DataAccess, iBizInfo
    {
      
        public List<BizInfo> GetBizs()
        {
            List<BizInfo> lstBizInfo= new List<BizInfo>();
            //Add rest of the code later

            return lstBizInfo;
        }
        public void DeleteBizInfo(int BizID)
        {
            try
            {
                SqlCommand cmd = GetSPCommand("DeleteBizInfo");
                cmd.Parameters.Add(new SqlParameter("@prmBizId", BizID));
                cmd.ExecuteNonQuery();

            }
            finally
            {
                CloseConnection();
            }

        }

        public void SaveBizInfo(BizInfo oBiz)
        {
            try
            {
                SqlCommand cmd = GetSPCommand("saveBizInfo");
                //  cmd.Parameters.Add(new SqlParameter("@prmBizID", oBiz.BizID));
                cmd.Parameters.Add(new SqlParameter("@prmBizID", oBiz.BizID));
                cmd.Parameters.Add(new SqlParameter("@prmTitle", oBiz.Title));
                cmd.Parameters.Add(new SqlParameter("@prmBizTypeID", oBiz.BizTypeID));
                cmd.Parameters.Add(new SqlParameter("@prmAskingPriceLow", oBiz.AskingPriceLow));
                cmd.Parameters.Add(new SqlParameter("@prmAskingPriceHigh", oBiz.AskingPriceHigh));
                cmd.Parameters.Add(new SqlParameter("@prmIsRelocatable", oBiz.IsRelocatable));
                cmd.Parameters.Add(new SqlParameter("@prmIsHomeBased", oBiz.IsHomeBased));
                cmd.Parameters.Add(new SqlParameter("@prmIsRealEstateAvial", oBiz.IsRealEstateAvail));
                cmd.Parameters.Add(new SqlParameter("@prmIsAssetSale", oBiz.IsAssetSale));
                cmd.Parameters.Add(new SqlParameter("@prmIsOwnerFinancing", oBiz.IsOwnerFinancing));
                cmd.Parameters.Add(new SqlParameter("@prmIsAbsenteeRun", oBiz.IsAbsenteeRun));
                cmd.Parameters.Add(new SqlParameter("@prmIsRealEstateIncluded", oBiz.IsRealEstateIncluded));
                cmd.Parameters.Add(new SqlParameter("@prmIsFSBO", oBiz.IsFSBO));
                cmd.Parameters.Add(new SqlParameter("@prmIsBrokerCoop", oBiz.IsBrokerCoop));
                cmd.Parameters.Add(new SqlParameter("@prmIsTrainingAndSupportAvial", oBiz.IsTrainingAndSupportAvail));
                cmd.Parameters.Add(new SqlParameter("@prmIsFinancialAssistanceAvial", oBiz.IsFinancialAssistanceAvail));
                cmd.Parameters.Add(new SqlParameter("@prmIsParternshipAvial", oBiz.IsParternshipAvail));
                cmd.Parameters.Add(new SqlParameter("@prmIsFranchise", oBiz.IsFranchise));
                cmd.Parameters.Add(new SqlParameter("@prmCountryID", oBiz.CountryID));
                cmd.Parameters.Add(new SqlParameter("@prmStateID", oBiz.StateID));
                cmd.Parameters.Add(new SqlParameter("@prmCountyID", oBiz.CountyID));
                cmd.Parameters.Add(new SqlParameter("@prmCityID", oBiz.CityID));
                cmd.Parameters.Add(new SqlParameter("@prmGrossIncome", oBiz.GrossIncome));
                cmd.Parameters.Add(new SqlParameter("@prmCashFlow", oBiz.CashFlow));
                cmd.Parameters.Add(new SqlParameter("@prmEBITDA", oBiz.EBITDA));
                cmd.Parameters.Add(new SqlParameter("@prmFurnitureAndFixtures", oBiz.FurnitureAndFixtures));
                cmd.Parameters.Add(new SqlParameter("@prmInventory", oBiz.Inventory));
                cmd.Parameters.Add(new SqlParameter("@prmYearEstablished", oBiz.YearEstablished));
                cmd.Parameters.Add(new SqlParameter("@prmNoOfEmployees", oBiz.NoOfEmployees));
                cmd.Parameters.Add(new SqlParameter("@prmReasonForSelling", oBiz.ReasonForSelling));
                cmd.Parameters.Add(new SqlParameter("@prmListedByUserID", oBiz.ListedByUserID));
                cmd.Parameters.Add(new SqlParameter("@prmDescription", oBiz.Description));
                cmd.Parameters.Add(new SqlParameter("@prmAddress1", oBiz.Address1));
                cmd.Parameters.Add(new SqlParameter("@prmCreatedDT", oBiz.CreatedDT));
                cmd.Parameters.Add(new SqlParameter("@prmUpdatedDT", oBiz.UpdatedDT));
                cmd.Parameters.Add(new SqlParameter("@prmIsActive", oBiz.IsActive));
                cmd.Parameters.Add(new SqlParameter("@prmIsSold", oBiz.IsSold));
                cmd.Parameters.Add(new SqlParameter("@prmViewedCounter", oBiz.ViewedCounter));
                cmd.Parameters.Add(new SqlParameter("@prmIsForBuy", oBiz.IsForBuy));
                cmd.Parameters.Add(new SqlParameter("@prmZip", oBiz.Zip));
                cmd.Parameters.Add(new SqlParameter("@prmAddress2", oBiz.Address2));
                cmd.Parameters.Add(new SqlParameter("@prmPostedDate", oBiz.PostedDate));
                cmd.Parameters.Add(new SqlParameter("@prmInActiveDate", oBiz.InActiveDate));
                cmd.Parameters.Add(new SqlParameter("@prmStatus", oBiz.Status));


                cmd.ExecuteNonQuery();

            }
            finally
            {
                CloseConnection();
            }
        }
   
    
    
    
    }

    public class UserDAL : DataAccess, iUserInfo
    {

        public List<UserInfo> GetUsers()
        {
            List<UserInfo> lstUserInfo = new List<UserInfo>();
            //Add rest of the code later

            return lstUserInfo;
        }
        public void DeleteUserInfo(int UserID)
        {
            try
            {
                SqlCommand cmd = GetSPCommand("DeleteUserInfo");
                cmd.Parameters.Add(new SqlParameter("@prmUserId", UserID));
                cmd.ExecuteNonQuery();

            }
            finally
            {
                CloseConnection();
            }

        }

        public void SaveUserInfo(UserInfo oUser)
        {
            try
            {
                SqlCommand cmd = GetSPCommand("saveUserInfo");
                //  cmd.Parameters.Add(new SqlParameter("@prmUserID", oUser.UserID));
                cmd.Parameters.Add(new SqlParameter("@prmUserID", oUser.UserID));
                cmd.Parameters.Add(new SqlParameter("@prmPassword", oUser.Password));
                cmd.Parameters.Add(new SqlParameter("@prmFirstName", oUser.FirstName));
                cmd.Parameters.Add(new SqlParameter("@prmLastName", oUser.LastName));
                cmd.Parameters.Add(new SqlParameter("@prmCompanyName", oUser.CompanyName));
                cmd.Parameters.Add(new SqlParameter("@prmContactOfficePhone", oUser.ContactOfficePhone));
                cmd.Parameters.Add(new SqlParameter("@prmContactMobilePhone", oUser.ContactMobilePhone));
                cmd.Parameters.Add(new SqlParameter("@prmContactEmail", oUser.ContactEmail));
                cmd.Parameters.Add(new SqlParameter("@prmLicenseNumber", oUser.LicenseNumber));
                cmd.Parameters.Add(new SqlParameter("@prmAddressLine1", oUser.AddressLine1));
                cmd.Parameters.Add(new SqlParameter("@prmAddressLIne2", oUser.AddressLIne2));
                cmd.Parameters.Add(new SqlParameter("@prmCityID", oUser.CityID));
                cmd.Parameters.Add(new SqlParameter("@prmStateID", oUser.StateID));
                cmd.Parameters.Add(new SqlParameter("@prmCountyID", oUser.CountyID));
                cmd.Parameters.Add(new SqlParameter("@prmCountryID", oUser.CountryID));
                cmd.Parameters.Add(new SqlParameter("@prmProfilePictureExists", oUser.ProfilePictureExists));
                cmd.Parameters.Add(new SqlParameter("@prmCreatedDT", oUser.CreatedDT));
                cmd.Parameters.Add(new SqlParameter("@prmUpdatedDT", oUser.UpdatedDT));
                cmd.Parameters.Add(new SqlParameter("@prmLoginCounter", oUser.LoginCounter));
                cmd.Parameters.Add(new SqlParameter("@prmZip", oUser.Zip));
                cmd.Parameters.Add(new SqlParameter("@prmSecAnswer1", oUser.SecAnswer1));
                cmd.Parameters.Add(new SqlParameter("@prmSecAnswer2", oUser.SecAnswer2));
                cmd.Parameters.Add(new SqlParameter("@prmSecAnswer3", oUser.SecAnswer3));
                cmd.Parameters.Add(new SqlParameter("@prmLastLoginDateTime", oUser.LastLoginDateTime));

                
                cmd.ExecuteNonQuery();

            }
            finally
            {
                CloseConnection();
            }
        }
        
    }

    
    public class DataAccess
    {
        [NonSerialized]
        protected SqlConnection connection;
        [NonSerialized]
        protected SqlCommand command;
        public static SqlDouble ToSqlDouble(Object ID)
        {
            SqlDouble siID;

            if (ID == DBNull.Value)
            {
                siID = SqlDouble.Null;
            }
            else
            {
                if (ID is SqlDouble)
                    return (SqlDouble)ID;
                siID = new SqlDouble((double)ID);
            }

            return siID;
        }

        public static SqlBoolean ToSqlBoolean(Object ID)
        {
            SqlBoolean siID;

            if (ID == DBNull.Value)
            {
                siID = SqlBoolean.Null;
            }
            else
            {
                if (ID is SqlBoolean)
                    return (SqlBoolean)ID;
                siID = new SqlBoolean((bool)ID);
            }

            return siID;
        }

        public static SqlInt32 ToSqlInt32(Object ID)
        {
            SqlInt32 siID;

            if (ID == DBNull.Value)
            {
                siID = SqlInt32.Null;
            }
            else
            {
                if (ID is SqlInt32)
                    return (SqlInt32)ID;
                siID = new SqlInt32(Convert.ToInt32(ID));
            }

            return siID;
        }

        public static SqlGuid ToSqlGuid(Object ID)
        {
            SqlGuid siID;

            if (ID == DBNull.Value)
            {
                siID = SqlGuid.Null;
            }
            else
            {
                if (ID is SqlGuid)
                    return (SqlGuid)ID;
                siID = new SqlGuid(new Guid(ID.ToString()));
            }

            return siID;
        }

        public static SqlDecimal ToSqlDecimal(Object ID)
        {
            SqlDecimal siID;

            if (ID == DBNull.Value)
            {
                siID = SqlDecimal.Null;
            }
            else
            {
                if (ID is SqlDecimal)
                    return (SqlDecimal)ID;
                siID = new SqlDecimal(Convert.ToDecimal(ID));
            }

            return siID;
        }

        public static SqlMoney ToSqlMoney(Object s)
        {
            SqlMoney siMoney;

            if (s == DBNull.Value)
            {
                siMoney = SqlMoney.Null;
            }
            else
            {
                if (s is SqlMoney)
                    return (SqlMoney)s;
                siMoney = new SqlMoney(Convert.ToDecimal(s));
            }

            return siMoney;
        }

        public static SqlDateTime ToSqlDateTime(Object s)
        {
            SqlDateTime sDateTime;

            if (s == DBNull.Value)
            {
                sDateTime = SqlDateTime.Null;
            }
            else
            {
                if (s is SqlDateTime)
                    return (SqlDateTime)s;
                sDateTime = new SqlDateTime(Convert.ToDateTime(s));
            }

            return sDateTime;
        }

        public static string ToString(Object s)
        {

            if (s == DBNull.Value)
            {
                return String.Empty;
            }
            else
            {
                return (string)s;
            }
        }
        public SqlCommand GetSPCommand(string storedProcedure)
        {
            connection = new SqlConnection(Settings.ConnectionString);
            command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = storedProcedure;
            //command.CommandTimeout  = ;
            connection.Open();
            return command;
        }
        public void CloseConnection()
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                connection.Close();
                connection.Dispose();
            }
        }
    }
    public static class Settings
    {
        public static string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conBizUser"].ConnectionString;
        //public static string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
        public static int PageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PageSize"]);
        
    }
}
